package org.ccu.eshop.service.impl;

import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties;
import org.ccu.eshop.dao.ShopCartDao;
import org.ccu.eshop.domain.dto.CartSelectedMerVo;
import org.ccu.eshop.domain.dto.CartVo;
import org.ccu.eshop.domain.dto.MerchandiseVo;
import org.ccu.eshop.domain.dto.OrdersVo;
import org.ccu.eshop.service.MerchandiseServiceltface;
import org.ccu.eshop.service.OrderServiceltface;
import org.ccu.eshop.service.ShopCarServiceltface;
import org.ccu.eshop.utils.DateFormateUtil;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

public class ShopCarServiceimpl implements ShopCarServiceltface {

  ShopCartDao shopCartDao = new ShopCartDao();
  MerchandiseServiceltface merchandiseService = new MerchandiseServiceImpl();
  OrderServiceltface orderService=new OrderServiceimpl();

  /**
   * 获得购物车
   *
   * @param member
   * @return
   */
  @Override
  public CartVo getShopCar(Integer member) {
    // 从数据库中查找是否存在
    CartVo cartVo = shopCartDao.selectCartByMember(member);
    if (cartVo == null) {
      shopCartDao.insert(member);
      cartVo = shopCartDao.selectCartByMember(member);
    }
    return cartVo;
  }

  /**
   * 按照购物车编号获得购物车里的商品详情
   *
   * @param cart
   * @return
   */
  @Override
  public List<CartSelectedMerVo> getCarSelectMerListByCar(Integer cart) {
    return shopCartDao.selectCartSelectMerListByCar(cart);
  }

  /**
   * 把商品添加到购物车
   *
   * @param cart
   * @param merId
   * @param favourable
   * @return
   */
  @Override
  public boolean addShopCar(Integer cart, String merId, Integer favourable) {
    // 获得商品信息
    MerchandiseVo merchandiseVo = merchandiseService.getMerchantById(merId);
    boolean b = false;
    CartSelectedMerVo cartSelectedMerVo = getCarSelectMerByCartAndMid(cart, merId);
    BigDecimal calcPrice = null;
    if (cartSelectedMerVo == null) {
      // 若该商品在购物车中不存在，添加该商品 插入操作
      // 打折后
      BigDecimal favourablePrice =
          merchandiseVo.getPrice().multiply(new BigDecimal(String.valueOf(favourable)));
      favourablePrice = favourablePrice.multiply(new BigDecimal("0.01"));
      if (merchandiseVo.getSpecial() == 1) {
        // 若是特价商品 比较特价和会员价 取最小值
        // 特价
        BigDecimal special = merchandiseVo.getSprice();
        // 比较
        if (special.compareTo(favourablePrice) == 1) {
          calcPrice = favourablePrice;
        } else {
          calcPrice = special;
        }
      } else {
        // 若是新品 就用会员价
        calcPrice = favourablePrice;
      }
      // 插入数据库
      b = addCartSelectedMer(cart, merchandiseVo.getId(), calcPrice);
    } else {
      // 若该商品在购物车中存在，商品数量+1 更新操作
      b = updateCartSelectedMerNumber(cartSelectedMerVo.getId(), cart); // 更新购物项 购物总金额
    }
    return b;
  }

  /**
   * 更新购物车内的商品项（针对在购物车中已存在的商品）
   *
   * @param id
   * @return
   */
  @Override
  public boolean updateCartSelectedMerNumber(Integer id, Integer cart) {
    // 更新商品项
    // 更新总金额
    return shopCartDao.updateCartSelectedMernumber(id, cart);
  }

  /**
   * 根据购物车编号 和商品编号 查询购物车详情
   *
   * @param cart
   * @param merId
   * @return
   */
  @Override
  public CartSelectedMerVo getCarSelectMerByCartAndMid(Integer cart, String merId) {
    // 查询
    int merchandise = Integer.parseInt(merId);
    return shopCartDao.selectCartSelectedMerByCarAndMid(cart, merchandise);
  }

  /**
   * 添加购物车内的商品项 商品个数为1
   *
   * @param cart
   * @param merchandise
   * @param price
   * @return
   */
  @Override
  public boolean addCartSelectedMer(Integer cart, Integer merchandise, BigDecimal price) {
    return shopCartDao.addCartSelectedMer(cart, merchandise, price);
  }

  /**
   * 删除购物车内的商品
   *
   * @param id
   * @return
   */
  @Override
  public boolean deleteCartSelectedMerById(String id, int cart) {
    int int_id = Integer.parseInt(id);
    return shopCartDao.deleteCartSelectedMerById(int_id, cart);
  }

  /**
   * 清空购物车
   *
   * @param cart
   * @return
   */
  @Override
  public boolean clearCartSelectedMerById(Integer cart) {

    return shopCartDao.deleteCartSelectedMerByCart(cart);
  }

  /**
   * 在网页的输入框上直接修改购物车中商品的数量
   *
   * @param id
   * @return
   */
  @Override
  public boolean updateCartSelectedMerNumById(String id, int num, int cart) {
    int int_id = Integer.parseInt(id);
    return shopCartDao.updateCartSelectedMerNumById(int_id, num, cart);
  }

  /**
   * 提交购物车
   *
   * @param cart
   * @param member
   * @return
   */
  @Override
  public OrdersVo submitShopCar(Integer cart, Integer member) {
    String orderNo = DateFormateUtil.getOrderNoByUUIDHashCode("");
    String orderDate = DateFormateUtil.formatDateString(new Date(),"yyyy-MM-dd HH:mm:ss") ;
    //提交购物车
    boolean b = shopCartDao.submitShopCar(cart, member, orderNo, orderDate);
    if (b) {
      return orderService.getOrdersByOrderNo(orderNo);
    }return  null;
  }
    }

